package com.fyqp.controller;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.fyqp.pojo.OprHistory;
import com.fyqp.pojo.Product;
import com.fyqp.pojo.ProductType;
import com.fyqp.service.IProductService;
import com.fyqp.service.IProductTypeService;
import com.fyqp.util.BaseResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author efire
 * @since 2022-02-05
 */
@RestController
@RequestMapping("/product")
@Slf4j
public class ProductController {
    @Resource
    private IProductService productService;

    @Resource
    private IProductTypeService productTypeService;

    @PostMapping("/oprPro")
    public BaseResult oprPro(@RequestBody Product product) {
        return productService.oprPro(product);
    }

    @PostMapping("/oprStock")
    public BaseResult oprStock(@RequestBody Product product) {
        return productService.oprStock(product);
    }

    @PostMapping("/query")
    public BaseResult queryInfo(@RequestBody Product product) {
        return productService.queryInfo(product);
    }

    @PostMapping("/history")
    public BaseResult queryOprHistory(@RequestBody OprHistory history) {
        return productService.queryOprHistory(history);
    }

    @PostMapping("/queryWithId")
    public BaseResult queryWithId(@RequestBody Product product) {
        return productService.queryWithId(product);
    }

    @PostMapping("/del")
    public BaseResult delPro(@RequestBody Product product) {
        return productService.delPro(product);
    }

    @GetMapping(value = "/export", produces = "application/octet-stream")
    public void exportProduct(HttpServletResponse response) {
        List<Product> products = productService.list();
        if (CollectionUtils.isNotEmpty(products)){
            for (Product product : products) {
                ProductType type = productTypeService.getById(product.getTypeId());
                if (type != null) {
                    product.setTypeName(type.getName());
                }
            }
        }
        ExportParams params = new ExportParams("产品信息", "产品信息", ExcelType.HSSF);
        Workbook workbook = ExcelExportUtil.exportExcel(params, Product.class, products);
        try {
            // 流形式
            response.setHeader("content-type", "application/octet-stream");
            // 防止中文乱码
            response.setHeader("content-disposition", "attachment;filename=" +
                    URLEncoder.encode("产品信息.xls", "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            log.error("encode error.");
        }
        try(ServletOutputStream outputStream = response.getOutputStream();) {
            workbook.write(outputStream);
        } catch (IOException e) {
            log.error("some errors happened when writing");
        }
    }


}
